#!/bin/bash

SELF_PATH=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

apt update && apt upgrade -y
apt install nginx apache2-utils curl -y

source $SELF_PATH/env.sh

mkdir -p ${FILE_PATH}
chown -R www-data:www-data ${FILE_PATH}

cat > /etc/nginx/sites-available/fileshare <<EOF
server {
  listen ${PORT};
  server_name ${IP};

  location / {
    root ${FILE_PATH};
    autoindex on;
    auth_basic "Files access";
    auth_basic_user_file ${AUTH_FILE};

    location ~* \.txt$ {
      add_header Content-Disposition 'attachment';
    }
  }
}
EOF

ln -s /etc/nginx/sites-available/fileshare /etc/nginx/sites-enabled/fileshare

htpasswd -b -c ${AUTH_FILE} ${USER} ${PASSWORD}

chmod 644 ${AUTH_FILE}

systemctl restart nginx
